package indi.jp.project.business.yunying.service.impl;

import indi.jp.project.business.yunying.dao.MaterialFiledDAO;
import indi.jp.project.business.yunying.entity.MaterialFiled;
import indi.jp.project.center.service.TaskService;
import indi.jp.project.core.entity.Datagrid;
import indi.jp.project.core.entity.JsonResult;
import indi.jp.project.core.entity.po.AttachmentPO;
import indi.jp.project.core.service.AttachmentService;
import indi.jp.project.core.util.SerialUtil;
import indi.jp.project.process.service.FlowTaskService;
import indi.jp.project.business.yunying.entity.MaterialFiledItem;
import indi.jp.project.business.yunying.service.MaterialFiledService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * 耗材Service
 */
@Service
public class MaterialFiledServiceImpl implements MaterialFiledService {

    @Resource
    private MaterialFiledDAO materialFiledDAO;

    @Resource
    private SerialUtil serialUtil;

    @Resource
    private FlowTaskService flowTaskService;

    @Resource
    private AttachmentService attachmentService;

    @Override
    public JsonResult queryGrid(MaterialFiled materialFiled) throws Exception {
        List<MaterialFiled> list = materialFiledDAO.queryList(materialFiled);
        int count = materialFiledDAO.queryCount(materialFiled);
        Datagrid<MaterialFiled> datagrid = new Datagrid<>(list, count);
        return JsonResult.success(datagrid);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult save(MaterialFiled materialFiled) throws Exception {
        String id;
        if (StringUtils.isBlank(materialFiled.getId())) {
            id = String.valueOf(serialUtil.nextId());
            materialFiled.setId(id);
            materialFiledDAO.insert(materialFiled);
        } else {
            id = materialFiled.getId();
            materialFiledDAO.update(materialFiled);
            // 删除材料表
            materialFiledDAO.deleteItemByBillId(id);
        }
        List<MaterialFiledItem> items = materialFiled.getItems();
        for (MaterialFiledItem item : items) {
            item.setId(String.valueOf(serialUtil.nextId()));
            item.setBillId(id);
        }
        materialFiledDAO.insertItems(items);
        List<AttachmentPO> attachments = materialFiled.getAttachments();
        if (attachments != null && attachments.size() > 0) {
            attachmentService.delete(id);
            attachmentService.save(attachments, id);
        }
        flowTaskService.saveTaskFlow(id, materialFiled.getFlowList());
        return JsonResult.success("保存成功");
    }

    @Override
    public JsonResult findById(String id) throws Exception {
        MaterialFiled materialFiled = materialFiledDAO.findById(id);
        List<MaterialFiledItem> items = materialFiledDAO.queryItems(id, materialFiled.getProjectId());
        List<AttachmentPO> attachments = attachmentService.queryAttachments(id, null);
        materialFiled.setItems(items);
        materialFiled.setAttachments(attachments);
        return JsonResult.success(materialFiled);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult deleteById(String id) throws Exception {
        materialFiledDAO.deleteById(id);
        materialFiledDAO.deleteItemByBillId(id);
        flowTaskService.deleteFlowByTaskId(id);
        attachmentService.delete(id);
        return JsonResult.success("删除成功");
    }


}
